package com.wang.users.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wang.common.pojo.ApiResponse;
import com.wang.common.pojo.ApiResponseEnum;
import com.wang.common.pojo.JSONEnum;
import com.wang.common.util.ConstatFinalUtil;
import com.wang.common.util.EncryptUtil;
import com.wang.common.util.PageInfoUtil;
import com.wang.users.dao.IABodyStandDao;
import com.wang.users.dao.IABodysideDao;
import com.wang.users.pojo.ABodyStand;
import com.wang.users.pojo.ABodyside;
import com.wang.users.pojo.Admins;
import com.wang.users.service.IABodysideService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Service("bodysideService")
public class BodysideServiceImpl implements IABodysideService {

    @Autowired
    private IABodysideDao bodysideDao;
    @Autowired
    private IABodyStandDao bodystandDao;
    

    @Override
    public ApiResponse<Object> saveOneBodysideService(ABodyside bodyside) {

        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        Map<String,Object> condMap = new HashMap<String, Object>();
        condMap.clear();
        condMap.put("usersId", bodyside.getUsersId());
        condMap.put("standId", bodyside.getStandId());
        ApiResponse<ABodyside> apiRes = this.findOneBodysideService(condMap);
        ABodyside bodysideRes = apiRes.getDataOneJava();
        
        if(bodysideRes != null)
        {
            /* 用户名已经存在,请换一个 */
            apiResponse.setCode(ApiResponseEnum.INFO_RECORD_EXISTS.getStatus());
            apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
            return apiResponse ;
        }
       
       double bml = bodyside.getWeight()  / (bodyside.getHeight() * bodyside.getHeight());
       
        /* 设置体脂 */
        bodyside.setBml(bml);
        
        bodyside.setCreateTime(new Date());
        bodyside.setUpdateTime(new Date());
        bodyside.setPubTime(new Date());

        int res = this.bodysideDao.save(bodyside);
        if (res > 0)
        {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            dataMap.put(JSONEnum.ID.getName(), bodyside.getId());
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else
        {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),Collections.EMPTY_MAP);
        return apiResponse;

    }

    @Override
    public ApiResponse<Object> updateOneBodysideService(ABodyside bodyside) {

        ApiResponse<Object> apiResponse = new ApiResponse<>();
        Map<String,Object> condMap = new HashMap<String,Object>();
        condMap.clear();
        condMap.put("usersId", bodyside.getUsersId());
        condMap.put("standId", bodyside.getStandId());
        ApiResponse<ABodyside> apiRes = this.findOneBodysideService(condMap);
        /*获取对象*/
        ABodyside aBodyside = apiRes.getDataOneJava();
        if (aBodyside != null && aBodyside.getId() != bodyside.getId()){

            apiResponse.setCode(ApiResponseEnum.INFO_RECORD_EXISTS.getStatus());
            apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
            return apiResponse ;
        }

        /* 设置健康指数 */
        double healthIndex = bodyside.getBmlFraction() * 0.15 + bodyside.getVitalcapacityFraction() * 0.15 + bodyside.getAbdominalcurlFraction() * 0.1 +
        		bodyside.getLongrunFraction() * 0.2 + bodyside.getShortrunFraction() * 0.2 + bodyside.getSittingforwardFraction() * 0.2 ;
        
        bodyside.setHealthIndex(healthIndex);
        
        bodyside.setUpdateTime(new Date());
        int res = this.bodysideDao.update(bodyside);
        if (res > 0)
        {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            dataMap.put(JSONEnum.ID.getName(), bodyside.getId());
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else
        {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<Object> deleteOneBodysideService(Map<String, Object> condMap) {

        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        int res = this.bodysideDao.delete(condMap);
        if (res > 0)
        {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else
        {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<ABodyside> findOneBodysideService(Map<String, Object> condMap) {
        ApiResponse<ABodyside> apiResponse = new ApiResponse<ABodyside>();

        Map<String, ABodyside> data = new HashMap<String, ABodyside>();
        ABodyside bodyside = this.bodysideDao.findOne(condMap);
        data.put(ApiResponseEnum.NAME_ONE.getName(), bodyside);
        apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        /* 将结果放到JSON中 */

        apiResponse.setDataOne(data);
        apiResponse.setDataOneJava(bodyside);
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<ABodyside> findCondListService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
        ApiResponse<ABodyside> apiResponse = new ApiResponse<ABodyside>();
        apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        List<ABodyside> demoABodysideList = Collections.EMPTY_LIST;
        
        if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + "")) {
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}

        Map<String, List<ABodyside>> dataList = new HashMap<String, List<ABodyside>>();
        if(pageInfoUtil != null){
            Page<Object> page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
            demoABodysideList = this.bodysideDao.findCondList(condMap);
            pageInfoUtil.setTotalRecord(Long.valueOf(page.getTotal()).intValue());
        }else {
            demoABodysideList = this.bodysideDao.findCondList(condMap);
        }

        /* 将结果放到JSON中 */
        dataList.put(ApiResponseEnum.NAME_LIST.getName(), demoABodysideList);

        apiResponse.setPageInfoUtil(pageInfoUtil);
        apiResponse.setDataListJava(demoABodysideList);
        apiResponse.setDataList(dataList);
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

	@Override
	public ApiResponse<Object> saveOneBodyStandService(ABodyStand bodystand)
	{

		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		/* 查询条件 */
		Map<String,Object> condMap=new HashMap<String,Object>();
		condMap.clear();
		condMap.put("grade", bodystand.getGrade());
		condMap.put("sex", bodystand.getSex());
		ApiResponse<ABodyStand> apiRes = this.findOneBodyStandService(condMap);
		/* 获取对象 */
		ABodyStand bodystandRes = apiRes.getDataOneJava() ; 
		if(bodystandRes != null)
		{
			/* 用户名已经存在,请换一个 */
			apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_EXISTS.getStatus());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
			return apiResponse ; 
		}
		
		bodystand.setCreateTime(new Date());
		bodystand.setUpdateTime(new Date());
		bodystand.setPubTime(new Date());
		
		int res = this.bodystandDao.save(bodystand);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), bodystand.getId());
			apiResponse.setDataOne(dataMap);
			
			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),Collections.EMPTY_MAP);
		return apiResponse;
	
	}

	@Override
	public ApiResponse<Object> updateOneBodyStandService(ABodyStand bodystand)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		/* 查询条件 */
		Map<String,Object> condMap=new HashMap<String,Object>();
		condMap.clear();
		condMap.put("grade", bodystand.getGrade());
		condMap.put("sex", bodystand.getSex());
		ApiResponse<ABodyStand> apiRes = this.findOneBodyStandService(condMap);
		/* 获取对象 */
		ABodyStand bodystandRes = apiRes.getDataOneJava() ; 
		if(bodystandRes != null && bodystandRes.getId() != bodystandRes.getId())
		{
			/* 用户名已经存在,请换一个 */
			apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_EXISTS.getStatus());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
			return apiResponse ; 
		}
		
		bodystand.setUpdateTime(new Date());
		int res = this.bodystandDao.update(bodystand);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), bodystand.getId());
			apiResponse.setDataOne(dataMap);
			
			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> deleteOneBodyStandService(Map<String, Object> condMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.bodystandDao.delete(condMap);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			apiResponse.setDataOne(dataMap);
			
			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<ABodyStand> findOneBodyStandService(Map<String, Object> condMap)
	{
		ApiResponse<ABodyStand> apiResponse = new ApiResponse<ABodyStand>();

		Map<String, ABodyStand> data = new HashMap<String, ABodyStand>();
		ABodyStand bodystand = this.bodystandDao.findOne(condMap);
		data.put(ApiResponseEnum.NAME_ONE.getName(), bodystand);
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		/* 将结果放到JSON中 */
		
		apiResponse.setDataOne(data);
		apiResponse.setDataOneJava(bodystand);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<ABodyStand> findCondListBodyStandService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		ApiResponse<ABodyStand> apiResponse = new ApiResponse<ABodyStand>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<ABodyStand> demobodystandList = Collections.EMPTY_LIST;
		
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + "")) {
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}

		Map<String, List<ABodyStand>> dataList = new HashMap<String, List<ABodyStand>>();
		if(pageInfoUtil != null){
			Page<Object> page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			demobodystandList = this.bodystandDao.findCondList(condMap);
			pageInfoUtil.setTotalRecord(Long.valueOf(page.getTotal()).intValue());
		}else {
			demobodystandList = this.bodystandDao.findCondList(condMap);
		}
		
		/* 将结果放到JSON中 */
		dataList.put(ApiResponseEnum.NAME_LIST.getName(), demobodystandList);

		apiResponse.setPageInfoUtil(pageInfoUtil);
		apiResponse.setDataListJava(demobodystandList);
		apiResponse.setDataList(dataList);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}   
}
